package com.xunyin.utils;

import com.xunyin.constant.DeviceConst;
import com.xunyin.enums.CommandCodeEnum;
import com.xunyin.enums.CtrDeviceChannelEnum;
import io.netty.channel.ChannelHandlerContext;

public class TCPServerUtil {
    /**
     * 根据控制卡地址、
     *
     * @author jthong
     * @Version 1.0.0
     * @Date 2019/12/26 16:28
     */
    public static String generateCommand(String ctrDeviceAddr, CtrDeviceChannelEnum ctrDeviceChannelEnum, CommandCodeEnum commandCodeEnum) {
//        String ctrDeviceAddr = ctrDeviceAddrEnum.getName();
        int commandCode = commandCodeEnum.getValue();

        StringBuffer commandBuff = new StringBuffer();
        // 设置指令头
        if (commandCode == CommandCodeEnum.OPEN.getIndex()) {
            commandBuff.append(DeviceConst.COMMAND_HEAD_OPEN);
        }
        if (commandCode == CommandCodeEnum.QUERY.getIndex()) {
            commandBuff.append(DeviceConst.COMMAND_HEAD_QUERY);
        }
        // 设置控制板地址
        commandBuff.append(ctrDeviceAddr);
        // 设置控制板通道地址
        commandBuff.append(ctrDeviceChannelEnum.getName());
        // 设置控制板操作指令
        commandBuff.append(commandCode);
        //计算BCC校验位
        commandBuff.append(BCCUtil.getBCC(commandBuff.toString()));
        return commandBuff.toString();
    }

    /**
     * 16进制表示的字符串转换为字节数组
     *
     * @param s 16进制表示的字符串
     * @return byte[] 字节数组
     */
    public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] b = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            // 两位一组，表示一个字节,把这样表示的16进制字符串，还原成一个字节
            b[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character
                    .digit(s.charAt(i + 1), 16));
        }
        return b;
    }

    public static String bytesToHexString(byte[] bArray, int length) {
        StringBuffer sb = new StringBuffer(length);
        String sTemp;
        for (int i = 0; i < length; i++) {
            sTemp = Integer.toHexString(0xFF & bArray[i]);
            if (sTemp.length() < 2) {
                sb.append(0);
            }
            sb.append(sTemp.toUpperCase());
        }
        return sb.toString();
    }

    public static String getId(ChannelHandlerContext ctx) {
        String remoteAddress = ctx.channel().remoteAddress().toString();
        return remoteAddress.substring(1).replaceAll("[^a-zA-Z0-9]", "");
    }

    /**
     * 16进制转换成为string类型字符串
     *
     * @param s
     * @return
     */
    public static String hexStringToString(String s) {
        if (s == null || s.equals("")) {
            return null;
        }
        s = s.replace(" ", "");
        byte[] baKeyword = new byte[s.length() / 2];
        for (int i = 0; i < baKeyword.length; i++) {
            try {
                baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            s = new String(baKeyword, "UTF-8");
            new String();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return s;
    }
}
